/*
 *  Copyright 2011 Arngrimur Bjarnason 
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 



package org.arngrimur.deviationtable.compass;

import java.util.HashMap;	
import java.util.Map;

import org.arngrimur.deviationtable.IDegree;



/**
 * @author Arngrimur Bjarnason, arngrimurbjarnason@gmail.com
 * 
 */
public class Compass {

	/** Name of the compass */
	private String name;
	/**
	 * Reference values and read values map for the given compass. Reference gps
	 * degree as key, read compas degree as value.
	 * */
	private Map<IDegree, IDegree> mappedValues;

	public Compass(String name) {
		this.name = name;
		this.mappedValues = new HashMap<IDegree, IDegree>();
	}

	/** Gets the name of the compass */
	public String getName() {
		return this.name;
	}

	/** Gets the database name of this compass. 
	 * @return The name in lower case and whitespace is removed.
	 */
	public String getDbName(){
		String regexp = "\\s";
		String [] subStrings = this.name.toLowerCase().trim().split(regexp) ;
		StringBuilder builder = new StringBuilder();
		for (String s : subStrings){
			builder.append(s);
		}
		return builder.toString();
	}
	
	/**
	 * Add new measurement to the compass.
	 * 
	 * @param refValue
	 *            The read reference value in degrees. Must be a value between 0
	 *            and 360 degrees.
	 * @param deg
	 *            The read compass value in degrees. Must be a value between 0
	 *            and 360 degrees.
	 * @throws IllegalArgumentException
	 *             if the <code>refValue</code> or <code>readValue</code> is not
	 *             between 0 and 360 degrees.
	 */
	public void setReadValue(IDegree refValue, IDegree deg) {
		this.mappedValues.put(refValue, deg);
	}

	/**
	 * Gets the value associated with a reference value
	 * @param referenceValue The reference value key
	 * @return The read value or <code>null</code> if not existing
	 */
	public IDegree getReadValue(IDegree referenceValue) {
		return this.mappedValues.get(referenceValue);
	}

	/**
	* Removes a read value from this compass
	 * @param read The reference value to be removed from the compass
	 * together with its read value
	 */
	public void removeReadValue(IDegree read) {
		this.mappedValues.remove(read);
	}
	
	public IDegree getDegree(IDegree compassValue){
		return this.mappedValues.get(compassValue);
	}
	
	@Override
	public String toString() {
		return this.getName()+": " + this.mappedValues.toString();
	}
}
